package com.bridgeintelligent.tag.webserver.dim.mgmt.service.dimfree;

import com.bridgeintelligent.tag.constants.Permissions;
import com.bridgeintelligent.tag.utils.SecurityHelper;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.BaseCode;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimData;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimPage;
import com.bridgeintelligent.tag.webserver.dim.mgmt.api.DimRep;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 自定义标签审批控制层
 */
@Slf4j
@Api(tags = "自定义标签审批")
@RestController
@RequestMapping("/api/dimFreeApproval")
public class DimFreeApprovalController {

    @Autowired
    private IDimFreeApprovalService dimFreeApprovalService;

    /**
     * 新增自定义标签审批通过
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "新增自定义标签审批通过", notes = "自定义标签审批")
    @PostMapping("addPass")
    public DimRep addPass(String dimId) {
        dimFreeApprovalService.addPass(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 新增自定义标签提交审批
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "新增自定义标签提交审批", notes = "自定义标签审批")
    @PostMapping("addSubmit")
    public DimRep addSubmit(String dimId) {
        dimFreeApprovalService.addSubmit(dimId);
        if(SecurityHelper.containsPermission(Permissions.WORKFLOW_DEFINITION_10011.getCode())){
            try{
                dimFreeApprovalService.addPass(dimId);
            }catch (Exception e){
                log.error("自动审核 新增定制标签 出错", e);
            }
        }
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 审批驳回
     *
     * @param dimId
     * @param desc
     * @return
     */
    @ApiOperation(value = "审批驳回", notes = "自定义标签审批")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "dimId", value = "标签id"),
            @ApiImplicitParam(paramType = "query", name = "desc", value = "驳回原因")
    })
    @PostMapping("Reject")
    public DimRep reject(String dimId, String desc) {
        dimFreeApprovalService.reject(dimId, desc);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 重新提交审批
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "重新提交审批", notes = "自定义标签审批")
    @PostMapping("resubmit")
    public DimRep resubmit(String dimId) {
        dimFreeApprovalService.resubmit(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 删除自定义标签审批通过
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "删除自定义标签审批通过", notes = "自定义标签审批")
    @PostMapping("delPass")
    public DimRep delPass(String dimId) {
        dimFreeApprovalService.delPass(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 自定义标签清除数据审批通过
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "自定义标签清除数据审批通过", notes = "自定义标签审批")
    @PostMapping("clePass")
    public DimRep clePass(String dimId) {
        dimFreeApprovalService.clePass(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 修改自定义标签审批通过
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "修改自定义标签审批通过", notes = "自定义标签审批")
    @PostMapping("updatePass")
    public DimRep updatePass(String dimId) {
        dimFreeApprovalService.updatePass(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 审批列表
     *
     * @param pageNumber
     * @param pageSize
     * @param theme
     * @param dimName
     * @param approveType
     * @param approveState
     * @param createTimeStart
     * @param createTimeEnd
     * @param uptTimeStart
     * @param uptTimeEnd
     * @return
     */
    @ApiOperation(value = "审批列表", notes = "自定义标签审批")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "pageNumber", value = "页码"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数"),
            @ApiImplicitParam(paramType = "query", name = "theme", value = "标签主题"),
            @ApiImplicitParam(paramType = "query", name = "dimName", value = "标签名"),
            @ApiImplicitParam(paramType = "query", name = "approveType", value = "申请类型"),
            @ApiImplicitParam(paramType = "query", name = "approveState", value = "审批状态"),
            @ApiImplicitParam(paramType = "query", name = "createTimeStart", value = "创建时间筛选（开始）"),
            @ApiImplicitParam(paramType = "query", name = "createTimeEnd", value = "创建时间筛选（结束）"),
            @ApiImplicitParam(paramType = "query", name = "uptTimeStart", value = "更新时间筛选（开始）"),
            @ApiImplicitParam(paramType = "query", name = "uptTimeEnd", value = "更新时间筛选（结束）")
    })
    @GetMapping("findList")
    public DimRep findList(Integer pageNumber, Integer pageSize, String theme, String dimName, String approveType, String approveState, String createTimeStart, String createTimeEnd, String uptTimeStart, String uptTimeEnd) {
        pageNumber = pageNumber == null ? 1 : pageNumber;
        pageSize = pageSize == null ? 10 : pageSize;
        DimPage<DimData> dataDimPage = dimFreeApprovalService.findList(pageNumber, pageSize, theme, dimName, approveType, approveState, createTimeStart, createTimeEnd, uptTimeStart, uptTimeEnd);
        return new DimRep(dataDimPage, BaseCode.SUCCESS);
    }

    /**
     * 获取所有的申请类型
     *
     * @return
     */
    @ApiOperation(value = "获取所有的申请类型", notes = "自定义标签审批")
    @GetMapping("findApproveType")
    public DimRep findApproveType() {
        Map map = dimFreeApprovalService.findApproveType();
        return new DimRep(map, BaseCode.SUCCESS);
    }

    /**
     * 上传新增审批通过
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "上传新增审批通过", notes = "自定义标签审批")
    @PostMapping("fileAddPass")
    public DimRep fileAddPass(String dimId) {
        dimFreeApprovalService.fileAddPass(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

    /**
     * 上传新增审批通过
     *
     * @param dimId
     * @return
     */
    @ApiOperation(value = "上传新增审批通过", notes = "自定义标签审批")
    @PostMapping("fileDeletePass")
    public DimRep fileDeletePass(String dimId) {
        dimFreeApprovalService.fileDeletePass(dimId);
        return new DimRep(BaseCode.SUCCESS);
    }

}
